<template>
  <div class="login">
    <van-nav-bar>
      <template #left>
        <div class="left-box">
          <div class="logo">
            <img src="../assets/home_active.png" alt="" class="auto-img" />
            <div class="logo-text">Luckin Coffee</div>
          </div>
        </div>
      </template>
      <template #right>
        <div class="home-text" @click="goPage('Home')">首页</div>
      </template>
    </van-nav-bar>

    <div class="login-box">
      <div class="welcome-title">欢迎回来!</div>
      <div class="welcome-subtitle">Please login to your accounts</div>

      <div class="form-box">
        <van-form>
          <van-field
            v-model="loginUserInfo.phone"
            name="手机号"
            label="手机号"
            placeholder="手机号"
            autocomplate="off"
          ></van-field>

          <van-field
            v-model="loginUserInfo.password"
            :type="!isOpenLogin ? 'password' : 'text'"
            name="密码"
            label="密码"
            placeholder="密码"
            :right-icon="!isOpenLogin ? 'closed-eye' : 'eye-o'"
            autocomplate="off"
            @click-right-icon="toggleLoginPassword"
          ></van-field>

          <div class="forgot">
            <div class="forgot-pwd">忘记密码?</div>
          </div>
          <div class="login-btn">
            <van-button round block color="#0C34BA" @click="login">
              登陆
            </van-button>
          </div>
          <div class="login-btn">
            <van-button round block type="default" @click="showPopup"
              >注册</van-button
            >
          </div>
        </van-form>
      </div>
    </div>

    <van-popup v-model="show" position="bottom" closeable>
      <div class="register-box">
        <div class="register-title">注册</div>
        <van-form>
          <van-field
            v-model="registerUserInfo.phone"
            name="手机号"
            label="手机号"
            placeholder="手机号"
            autocomplete="off"
          ></van-field>
          <van-field
            v-model="registerUserInfo.password"
            :type="!isOpenRegister ? 'password' : 'text'"
            name="密码"
            label="密码"
            placeholder="密码"
            :right-icon="!isOpenRegister ? 'closed-eye' : 'eye-o'"
            autocomplete="off"
            @click-right-icon="toggleResgiterPassword"
          ></van-field>

          <van-field
            v-model="loginUserInfo.nickName"
            type="text"
            name="昵称"
            label="昵称"
            placeholder="昵称"
            autocomplate="off"
          ></van-field>

          <div class="login-btn">
            <van-button round block color="#0C34BA" @click="register"
              >注册</van-button
            >
          </div>
        </van-form>
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      show: false,
      loginUserInfo: {
        phone: "",
        password: "",
      },
      registerUserInfo: {
        phone: "",
        password: "",
        nickName: "",
      },
      isOpenRegister: false,
      isOpenLogin: false,
    };
  },

  methods: {
    showPopup() {
      this.show = !this.show;
    },
    goPage(name) {
      this.$router.push({ name });
    },
    toggleResgiterPassword() {
      this.isOpenRegister = !this.isOpenRegister;
    },
    toggleLoginPassword() {
      this.isOpenLogin = !this.isOpenLogin;
    },

    register() {
      this.axios({
        method: "post",
        url: "/register",
        data: {
          ...this.registerUserInfo,
        },
      })
        .then((result) => {
          this.$toast(result.data.msg);
          if (result.data.code === 100) {
            this.show = false;
            for (let key in this.registerUserInfo) {
              this.registerUserInfo[key] = "";
            }
          }
        })
        .catch((err) => {
        });
    },

    login() {
      this.axios({
        method: "post",
        url: "/login",
        data: {
          ...this.loginUserInfo,
        },
      })
        .then((result) => {
          this.$toast(result.data.msg);
          if (result.data.code === 200) {
            localStorage.setItem("token", result.data.token);

            setTimeout(() => {
              this.$router.push({ name: "Home" });
            }, 1000);
          }
        })
        .catch((err) => {
        });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: #fff;
  .register-title {
    font-size: 24px;
    margin-bottom: 20px;
  }
  .register-box {
    padding: 10px;
  }
  .forgot {
    margin-top: 20px;
    display: flex;
  }
  .forgot-pwd {
    margin-left: auto;
    font-size: 14px;
    color: #666;
  }
  .login-btn {
    margin-top: 50px;
  }
  .form-box {
    margin-top: 50px;
  }
  .welcome-title {
    font-size: 30px;
  }
  .welcome-subtitle {
    font-size: 18px;
    margin-top: 20px;
    color: #999;
  }
  .login-box {
    margin-top: 80px;
    padding: 10px;
  }
  .home-text {
    color: #0c34ba;
    font-weight: bold;
    font-size: 14px;
  }
  .left-box {
    display: flex;
    align-items: center;
  }
  .logo {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    overflow: hidden;
  }
  .logo-text {
    margin-left: 10px;
    font-weight: bold;
    font-size: 14px;
  }
}
</style>